package com.ruoyi.receipt.controller;

import java.util.List;
import java.util.Map;

import com.ruoyi.receipt.domain.TbCaUser;
import com.ruoyi.receipt.service.ITbCaUserService;
import com.ruoyi.receipt.utils.CurrentUserUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.receipt.domain.TbUser;
import com.ruoyi.receipt.service.ITbUserService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.IdWorker;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 用户信息Controller
 * 
 * @author ruoyi
 * @date 2021-03-15
 */
@RestController
@RequestMapping("/receipt/user")
public class TbUserController extends BaseController
{
    @Autowired
    private ITbUserService tbUserService;

    @Autowired
    private IdWorker idWorker;

    @Autowired
    private ITbCaUserService tbCaUserService;

    /**
     * 查询用户信息列表
     */
    @PreAuthorize("@ss.hasPermi('receipt:user:list')")
    @GetMapping("/list")
    public TableDataInfo list(TbUser tbUser)
    {
        startPage();
        List<TbUser> list = tbUserService.selectTbUserList(tbUser);
        return getDataTable(list);
    }

    /**
     * 导出用户信息列表
     */
    @PreAuthorize("@ss.hasPermi('receipt:user:export')")
    @Log(title = "用户信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TbUser tbUser)
    {
        List<TbUser> list = tbUserService.selectTbUserList(tbUser);
        ExcelUtil<TbUser> util = new ExcelUtil<TbUser>(TbUser.class);
        return util.exportExcel(list, "user");
    }

    /**
     * 获取用户信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('receipt:user:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(tbUserService.selectTbUserById(id));
    }

    /**
     * 重置密码和密保
     */
    @PreAuthorize("@ss.hasPermi('receipt:user:reset')")
    @PostMapping("/resetUser")
    public AjaxResult resetUser(@RequestBody Long[] ids){
        for (int i = 0; i < ids.length; i++) {

        //将密码置空
        TbUser tbUser=new TbUser();
        tbUser.setId(ids[i]);
        tbUser.setPwd("");
        tbUser.setIsReset(1);
        tbUserService.updateTbUser(tbUser);

        //删除密保问题
        tbCaUserService.deleteTbCaUserByUserId(ids[i]);

        }

        return AjaxResult.success();
    }

    /**
     * 新增用户信息
     */
    @PreAuthorize("@ss.hasPermi('receipt:user:add')")
    @Log(title = "用户信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbUser tbUser)
    {
        tbUser.setId(idWorker.nextId());
        return toAjax(tbUserService.insertTbUser(tbUser));
    }

    /**
     * 修改用户信息
     */
    @PreAuthorize("@ss.hasPermi('receipt:user:edit')")
    @Log(title = "用户信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbUser tbUser)
    {
        return toAjax(tbUserService.updateTbUser(tbUser));
    }

    /**
     * 删除用户信息
     */
    @PreAuthorize("@ss.hasPermi('receipt:user:remove')")
    @Log(title = "用户信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tbUserService.deleteTbUserByIds(ids));
    }
}
